<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<text class="title">热销爆款</text>
		</view>
		
		<!-- 筛选栏 -->
		<view class="filter-bar">
			<view class="filter-item" @click="selectFilter('default')" :class="{'active': currentFilter === 'default'}">
				<text>综合</text>
			</view>
			<view class="filter-item" @click="selectFilter('sales')" :class="{'active': currentFilter === 'sales'}">
				<text>销量</text>
				<text class="arrow" :class="{'asc': salesOrder === 'asc', 'desc': salesOrder === 'desc'}"></text>
			</view>
			<view class="filter-item" @click="selectFilter('price')" :class="{'active': currentFilter === 'price'}">
				<text>价格</text>
				<text class="arrow" :class="{'asc': priceOrder === 'asc', 'desc': priceOrder === 'desc'}"></text>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<scroll-view 
			scroll-y 
			class="product-list"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="product-grid">
				<view 
					class="product-item" 
					v-for="(item, index) in sortedProducts" 
					:key="index"
					@click="navigateToDetail(item.id)"
				>
					<image :src="item.image" mode="aspectFill" class="product-image"></image>
					<view class="product-info">
						<text class="name">{{item.name}}</text>
						<text class="price">¥{{item.price}}</text>
						<view class="rating">
							<text class="score">{{item.rating}}</text>
							<text class="sales">已售{{item.sales}}</text>
						</view>
						<view class="hot-tag" v-if="index < 3">
							<text>TOP{{index + 1}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="products.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无商品</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'

// 筛选条件
const currentFilter = ref('default')
const salesOrder = ref('desc')
const priceOrder = ref('desc')

// 是否正在刷新
const isRefreshing = ref(false)

// 是否还有更多数据
const hasMore = ref(true)

// 商品列表
const products = ref([
	{
		id: 1,
		name: '川味红油火锅底料',
		price: '39.90',
		image: '/static/images/products/huoguo1.jpg',
		rating: '4.8',
		sales: '1000+'
	},
	{
		id: 4,
		name: '精选肥牛片',
		price: '68.00',
		image: '/static/images/products/meat1.jpg',
		rating: '4.9',
		sales: '2000+'
	},
	{
		id: 9,
		name: '有机生菜',
		price: '12.90',
		image: '/static/images/products/veg1.jpg',
		rating: '4.7',
		sales: '2000+'
	},
	{
		id: 10,
		name: '金针菇',
		price: '15.90',
		image: '/static/images/products/veg2.jpg',
		rating: '4.8',
		sales: '1800+'
	},
	{
		id: 6,
		name: '精品毛肚',
		price: '88.00',
		image: '/static/images/products/meat3.jpg',
		rating: '4.7',
		sales: '1500+'
	},
	{
		id: 13,
		name: '手工面条',
		price: '16.90',
		image: '/static/images/products/staple1.jpg',
		rating: '4.8',
		sales: '1500+'
	}
])

// 排序后的商品列表
const sortedProducts = computed(() => {
	let sorted = [...products.value]
	
	if (currentFilter.value === 'sales') {
		sorted.sort((a, b) => {
			const salesA = parseInt(a.sales.replace('+', ''))
			const salesB = parseInt(b.sales.replace('+', ''))
			return salesOrder.value === 'asc' ? salesA - salesB : salesB - salesA
		})
	} else if (currentFilter.value === 'price') {
		sorted.sort((a, b) => {
			const priceA = parseFloat(a.price)
			const priceB = parseFloat(b.price)
			return priceOrder.value === 'asc' ? priceA - priceB : priceB - priceA
		})
	}
	
	return sorted
})

// 选择筛选条件
const selectFilter = (filter) => {
	if (filter === currentFilter.value) {
		// 切换排序方向
		if (filter === 'sales') {
			salesOrder.value = salesOrder.value === 'asc' ? 'desc' : 'asc'
		} else if (filter === 'price') {
			priceOrder.value = priceOrder.value === 'asc' ? 'desc' : 'asc'
		}
	} else {
		currentFilter.value = filter
	}
}

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	uni.showLoading({
		title: '加载中...'
	})
	
	setTimeout(() => {
		// 模拟加载更多数据
		const newProducts = [
			{
				id: products.value.length + 1,
				name: '新品' + (products.value.length + 1),
				price: (Math.random() * 100 + 10).toFixed(2),
				image: '/static/images/products/default.jpg',
				rating: (Math.random() * 0.3 + 4.6).toFixed(1),
				sales: Math.floor(Math.random() * 1000) + '+'
			}
		]
		
		products.value.push(...newProducts)
		
		// 模拟没有更多数据
		if (products.value.length > 20) {
			hasMore.value = false
		}
		
		uni.hideLoading()
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	
	setTimeout(() => {
		// 随机更新销量数据
		products.value = products.value.map(item => ({
			...item,
			sales: Math.floor(Math.random() * 1000) + '+'
		}))
		
		isRefreshing.value = false
		
		uni.showToast({
			title: '刷新成功',
			icon: 'success'
		})
	}, 1000)
}

// 跳转到商品详情
const navigateToDetail = (id) => {
	uni.navigateTo({
		url: `/pages/product/detail?id=${id}`
	})
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
}

/* 导航栏样式 */
.nav-bar {
	background-color: #FFFFFF;
	height: 88rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border-bottom: 1rpx solid #EEEEEE;
	
	.title {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
	}
}

/* 筛选栏样式 */
.filter-bar {
	display: flex;
	padding: 0 30rpx;
	height: 80rpx;
	background-color: #FFFFFF;
	border-bottom: 1rpx solid #EEEEEE;
	
	.filter-item {
		display: flex;
		align-items: center;
		margin-right: 40rpx;
		
		text {
			font-size: 28rpx;
			color: #333;
		}
		
		&.active {
			color: #D02B29;
			font-weight: bold;
			
			text {
				color: #D02B29;
			}
		}
		
		.arrow {
			position: relative;
			width: 24rpx;
			height: 24rpx;
			margin-left: 8rpx;
			
			&::before,
			&::after {
				content: '';
				position: absolute;
				left: 50%;
				width: 0;
				height: 0;
				border-left: 6rpx solid transparent;
				border-right: 6rpx solid transparent;
			}
			
			&::before {
				top: 4rpx;
				border-bottom: 6rpx solid #CCCCCC;
			}
			
			&::after {
				bottom: 4rpx;
				border-top: 6rpx solid #CCCCCC;
			}
			
			&.asc::before {
				border-bottom-color: #D02B29;
			}
			
			&.desc::after {
				border-top-color: #D02B29;
			}
		}
	}
}

/* 商品列表样式 */
.product-list {
	height: calc(100vh - 168rpx);
}

.product-grid {
	padding: 20rpx;
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	
	.product-item {
		width: calc(50% - 10rpx);
		background-color: #FFFFFF;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		overflow: hidden;
		position: relative;
		
		.product-image {
			width: 100%;
			height: 320rpx;
			background-color: #F5F5F5;
		}
		
		.product-info {
			padding: 16rpx;
			
			.name {
				font-size: 26rpx;
				color: #333;
				font-weight: bold;
				line-height: 1.4;
				height: 72rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}
			
			.price {
				font-size: 32rpx;
				color: #D02B29;
				font-weight: bold;
				margin: 12rpx 0;
				
				&::before {
					content: '¥';
					font-size: 24rpx;
					margin-right: 2rpx;
				}
			}
			
			.rating {
				display: flex;
				align-items: center;
				justify-content: space-between;
				
				.score {
					font-size: 22rpx;
					color: #FFB800;
					
					&::after {
						content: '分';
						margin-left: 2rpx;
					}
				}
				
				.sales {
					font-size: 22rpx;
					color: #999;
				}
			}
		}
		
		.hot-tag {
			position: absolute;
			top: 0;
			left: 0;
			background: linear-gradient(135deg, #FF6B6B, #D02B29);
			padding: 8rpx 16rpx;
			border-radius: 0 0 12rpx 0;
			
			text {
				font-size: 22rpx;
				color: #FFFFFF;
				font-weight: bold;
			}
		}
	}
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx;
	
	text {
		font-size: 24rpx;
		color: #999;
	}
}

/* 空状态样式 */
.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 100rpx 0;
	
	.empty-img {
		width: 200rpx;
		height: 200rpx;
		margin-bottom: 20rpx;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999;
	}
}
</style> 